package com.zebraphp.devtool.SocketUtil;

import com.zebraphp.devtool.DebugBase;
import com.zebraphp.devtool.util.ILog;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2018/4/26.
 */
public class UDPSocketUtil {

    private static ExecutorService mExecutorService = null;
    private DatagramSocket mUdpSocket = null;

    private String IP;
    private int port;

    public static ExecutorService getExecutorsInstance() {
        if (mExecutorService == null) {
            mExecutorService = Executors.newCachedThreadPool();
        }
        return mExecutorService;
    }

    public void disconnect() {

    }

    public void connect(String IP, int port) {
        getExecutorsInstance().execute(new ConnectService(IP, port));
    }

    public void connect() {
        getExecutorsInstance().execute(new ConnectService());
    }

    public void send(String msg) {
        getExecutorsInstance().execute(new SendService(msg));
    }

    public void receiveMsg() {
        byte[] msgRec = new byte[1024];//接收消息
        DatagramPacket packetRec = new DatagramPacket(msgRec, msgRec.length);
        while (true) {
            try {
                mUdpSocket.receive(packetRec);
                String receiveMsg = new String(packetRec.getData(), packetRec.getOffset(), packetRec.getLength());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private class SendService implements Runnable {
        private String msg;

        public SendService(String msg) {
            this.msg = msg+"\n";
        }

        @Override
        public void run() {
            try {
                byte data[] = msg.getBytes();
                InetAddress serverAddress = InetAddress.getByName(IP);
                DatagramPacket packet = new DatagramPacket(data, data.length, serverAddress, port);
                mUdpSocket.send(packet);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private class ConnectService implements Runnable {

        public ConnectService() {
            IP = "192.168.1.1";
            port = 2001;
        }

        public ConnectService(String ip, int udpPort) {
            IP = ip;
            port = udpPort;
        }

        @Override
        public void run() {
            try {
                mUdpSocket = new DatagramSocket(port);
                mUdpSocket.setSoTimeout(3000);
                if (mUdpSocket.isConnected()) {
                    ILog.print(DebugBase.tagGeneral, "========= Socket UDP 连接成功 =========");
                } else {
                    ILog.print(DebugBase.tagGeneral, "========= Socket UDP 连接失败 =========");
                }
                //循环接收消息
                receiveMsg();
            } catch (SocketException e) {
                e.printStackTrace();
            }
        }
    }
}
